// pages/my/my.js
const sharePost = require('../../request/sharePost.js')

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    info: '',
    share: {},
    list: [],
    inviteList: [],
    code: '',
    endDays: '',
    invite: {
      user_name: '',
      user_phone: '',
      uid: ''
    },
    sDetails: {},
    isDetail: 'none',
    isUpdate:'',
    user_type: ''
  },
  showSingel(event) {
    this.setData({
      sDetails: event.currentTarget.dataset.detail,
      isDetail: '',
      isUpdate: ''
    })
  },
  closeClick(e) {
    this.setData({
      isDetail: e.detail.isDetail,
      isUpdate: e.detail.isUpdate
    })
    console.log(e.detail, 'isDetail', this.data.isDetail);
    console.log(e.detail, 'isUpdate', this.data.isUpdate);
    //修改后进行重新获取数据
    if (this.data.isUpdate=="true") {
      //重新获取信息
      let app = getApp();
      this.setData({
        userInfo: app.globalData.userInfo,
        info: app.globalData.info
      })
      console.log(this.data, 'info')
      this.init();
    }
  },
  /**
   * 生成邀请码
   */
  getInviteCode: function() {
    
   
    let params = {
      "openid": app.globalData.info.openid
    }
    console.log('查看open ID', params)
    sharePost.getInviteCode(params).then(res => {
      console.log('或取邀请码', res)
      this.setData({
        code: res.data.code
      })
    })
  },
  /**
   * 复制邀请码
   */
  textPaste() {
    wx.showToast({
      title: '复制成功',
    })
    wx.setClipboardData({
      data: this.data.code,
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            console.log(res.data) // data
          }
        })
      }
    })
  },
  turnTips: function() {
    wx.navigateTo({
      url: '../reward/reward',
    })
  },
  turnToCom: function() {
    console.log('user类型', this.data.info)
    let user_type = app.globalData.info.user_type
    let invite = {}
    invite.user_name = this.data.info.invite_name
    invite.user_phone = this.data.info.invite_phone
    invite.uid = this.data.info.sid
    this.setData({
      invite: invite,
      user_type: app.globalData.info.user_type
    })
    console.log('invite', this.data.invite)
    wx.navigateTo({
      url: '../complete/complete?user_type=' + user_type + '&invite=' + this.data.invite + '&invite_name=' + this.data.info.invite_name + '&invite_phone=' + this.data.info.invite_phone + '&sid=' + this.data.info.sid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let app = getApp();
    this.setData({
      userInfo: app.globalData.userInfo,
      info: app.globalData.info,
      user_type: app.globalData.info.user_type
    })
    console.log('my', app.globalData)
    this.init()
  },
  /**
   * 初始化
   */
  init: function() {
    console.log('this.data.info.user_type555', app.globalData, this.data.info.user_type)
    if (app.globalData.info.user_type != 1) {
      this.getOrderList()
    } else {
      this.getSalesmanOrderList()
    }
  },
  /**
   * 获取推荐好友预约列表(普通会员)
   */
  getOrderList: function() {
    let params = {
      uid: app.globalData.info._id,
      openid: app.globalData.info.openid
    }
    sharePost.getOrderList(params).then(res => {
      console.log('获取推荐好友预约列表', res)
      this.setData({
        share: res,
        list: res.data
      })
    })
  },
  /**
   * 获取推荐好友预约列表(业务员)
   */
  getSalesmanOrderList: function() {
    let params = {
      sid: app.globalData.info._id
    }
    sharePost.getSalesmanOrderList(params).then(res => {
      console.log('获取推荐好友预约列表', res)
      for (let i = 0; i < res.data.length; i++) {
        res.data[i].isTrue = false
      }
      this.setData({
        share: res,
        inviteList: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: 'Rainbow挚友优享',
      desc: 'Rainbow挚友优享。',
      path: '/pages/index/index'
    }
  },
  /**
   * 手风琴
   */
  open_that(event) {
    this.selectedIndex = event.currentTarget.dataset.index
    for (var i = 0; i < this.data.inviteList.length; i++) {
      if (this.selectedIndex != i) {
        this.data.inviteList[i].isTrue = false
      }
    }
    this.data.inviteList[this.selectedIndex].isTrue = !this.data.inviteList[this.selectedIndex].isTrue
    let inviteList = this.data.inviteList
    this.setData({
      inviteList: inviteList
    })
  },
})